package com.zhang.novel.core.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static String getCookie(HttpServletRequest request, String key) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals(key)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static void setCookie(HttpServletResponse response, String key, String value) {
        Cookie cookie=new Cookie(key,value);
        // 设置同一应用服务器内共享cookie
        // 跨域共享cookie的方法：设置cookie.setDomain(".xxx.com");
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
